package com.dog.modules.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dog.framework.core.model.PageResult;
import com.dog.framework.mybatis.service.impl.BaseServiceImpl;
import com.dog.framework.mybatis.utils.PageUtils;
import com.dog.modules.system.convert.SysAttachmentConvert;
import com.dog.modules.system.domain.SysAttachment;
import com.dog.modules.system.api.query.SysAttachmentQuery;
import com.dog.modules.system.api.vo.SysAttachmentVO;
import com.dog.modules.system.mapper.SysAttachmentMapper;
import com.dog.modules.system.service.SysAttachmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author liuxk
 * @description 针对表【sys_attachment(附件管理)】的数据库操作Service实现
 * @createDate 2024-01-30 14:47:29
 */
@Service
public class SysAttachmentServiceImpl extends BaseServiceImpl<SysAttachmentMapper, SysAttachment> implements SysAttachmentService {


    @Override
    public PageResult<SysAttachmentVO> page(SysAttachmentQuery query) {

        PageHelper.startPage(query.getPageNum(),query.getPageSize());
        List<SysAttachment> sysAttachmentList = this.list(getWrapper(query));
        PageInfo<SysAttachment> pageInfo = new PageInfo<>(sysAttachmentList);
        List<SysAttachmentVO> sysAttachmentVOList  = SysAttachmentConvert.INSTANCE.convertList(pageInfo.getList());
        return PageUtils.getPage(pageInfo,sysAttachmentVOList);
       // return  new PageResult<>(sysAttachmentVOList,pageInfo.getTotal());
    }

    private LambdaQueryWrapper<SysAttachment> getWrapper(SysAttachmentQuery query) {
        LambdaQueryWrapper<SysAttachment> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(query.getPlatform()), SysAttachment::getPlatform, query.getPlatform());
        wrapper.like(StrUtil.isNotBlank(query.getName()), SysAttachment::getName, query.getName());
        wrapper.orderByDesc(SysAttachment::getId);
        return wrapper;
    }

    @Override
    public void save(SysAttachmentVO vo) {
        SysAttachment entity = SysAttachmentConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(SysAttachmentVO vo) {
        SysAttachment entity = SysAttachmentConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }



}




